package com.canbot.u05.api;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.control.RobotMgr;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.socket.StringMsgBean;
import com.canbot.u05.utils.Logger;


/**
 * 处理胸口发送过来的消息数据
 */

public class ReceiveData {

        private static final String TAG = "ReceiveData";

        private Context mContext;

        private MyReceiveDataInterface listener;

        /**
         * 处理胸口发送过来的消息
         */
        private Handler mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                        String msgData = (String) msg.obj;
                        Logger.e(TAG, "msg.what " + msg.what + " msg.obj: " + msg.obj);
                        switch (msg.what) {
                                // 胸口平板通知更新
                                case MsgType.MOBILE_PAD_VERSION_UPDAT:
                                        if (msgData.isEmpty() || !msgData.endsWith("apk")) {
                                                return;
                                        }
                                        else {
                                        }
                                        break;
//                                //接受导览任务相关的结果
//                                case MsgType.MOBILE_PAD_MAP_TASK:
//                                        EventBus.getDefault().post(new StringMsgBean(MsgType.MOBILE_PAD_MAP_TASK, msgData));
//                                        break;
//                                //收到胸口返回的心跳消息
//                                case  9004:
//                                        Intent intent = new Intent();
//                                        intent.setAction(com.canbot.u05.utils.ConstUtils.ACTION_DISCONNECT_SUCCESS);
//                                        LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
//                                        break;

                        }
                }
        };


        public ReceiveData(Context context) {
                this.mContext = context;
                listener = new MyReceiveDataInterface();
                RobotMgr.getInstance().init(listener);
        }

        /**
         * 接收处理胸口发来的消息
         */
        public class MyReceiveDataInterface implements ReceiveDataInterface {

                @Override
                public void onReceive(String data) {
                        Logger.e(TAG, "ReceiveData data===" + data);

//            Looper.prepare();
//            Toast.makeText(mContext,data, Toast.LENGTH_SHORT).show();
//            Looper.loop();

                        if (data != null) {
                                StringMsgBean strMsgBean = JSON.parseObject(data, StringMsgBean.class);
                                if (strMsgBean != null) {
                                        int msgType = strMsgBean.getMsgType();
                                        String msgData = strMsgBean.getMsgData();
                                        Logger.e(TAG, "ReceiveData StringMsgBean===" + strMsgBean.toString() + ",msgType=  " + msgType);
                                        mHandler.obtainMessage(msgType, msgData).sendToTarget();
                                }
                        }
                }
        }
}
